#!/bin/sh
set -eu

if [ $# -lt 1 ]; then
  echo 'No task is specified.' >&2
  exit 1
fi

if [ "$(uname)" = 'Darwin' ]; then
  export CC="${CC:-$(brew --prefix)/opt/llvm/bin/clang}"
  export CXX="${CXX:-$(brew --prefix)/opt/llvm/bin/clang++}"
elif [ "$(uname)" = 'Linux' ]; then
  export CC="${CC:-clang}"
  export CXX="${CXX:-clang++}"
  if [ "$CXX" = 'clang++' ]; then
    export CXXFLAGS="${CXXFLAGS:--stdlib=libc++}"
  fi
fi

cd "$(dirname "$0")"

case $1 in
  'c'|'configure' )
    cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake
    ;;
  'b'|'build' )
    cmake --build build
    ;;
  't'|'test' )
    ctest -V --test-dir build
    ;;
  * )
    echo "Unrecognized task: $1" >&2
    exit 1
    ;;
esac
